/**
 * Created by LinJe on 2015/9/21.
 * 处理首页的抓取和发布数据
 */
(function( $ ) {

    var partinprodServer = {

        //数据浅拷贝
        'extend': function( defs, settings ) {
            for ( var attr in defs ) {
                if ( typeof settings[attr] != 'undefined' ) {
                    defs[attr] = settings[attr];
                }
            }
        },


        /**
         * common() 公共方法
         * @param settings
         * settings.type 默认值'POST', 请求的类型
         * settings.dataType 数据类型
         * settings.url 请求数据的地址
         * settings.data 要发送的数据
         * settings.callBack 请求之后的回调函数
         * 回调函数参数 msg 的数据格式如下：
         * {
         *      'data': msg, //成功提交之后返回的数据
         *      'status': 1, //0代表提交失败，1代表提交成功
         *      'error': '失败信息' //提交失败的信息提示
        }
         */
        'common': function( settings ) {
            //设置默认值
            var defs = {
                'type': 'POST',
                'dataType': 'json',
                'url': '',
                'data': {},
                'callBack': function(){}
            };

            //覆盖数据
            this.extend(defs, settings);

            //经过ajax处理表单数据之后返回结果
            $.ajax({
                type: defs.type,
                dataType: defs.dataType,
                url: defs.url,
                data: defs.data,
                success: function (msg) {
                    defs.callBack && defs.callBack(msg);
                },
                error: function(msg) {
                    defs.callBack && defs.callBack({'error':true, 'description':'服务器异常', 'data':msg});
                }
            });
        },


        //获取商品数据
        'getProductData': function( settings, callBack ) {

            this.common({
                'type': 'POST',
                'dataType': 'json',
                'url': '/participation/findParticipation',
                'data': {
                    //需要抓取数据的url
                    'pageStep':settings.pageStep,
                    'count':settings.count
                },
                'callBack': callBack
            });

            ///////////////////////////以下是前端测试效果用////////////////////////////
            //期望返回的json数据格式如下
            //微博数据
            /*
            var data = {
                "code": 200,
                "description": "sucess",
                "detail": {
                    "totalSize": "10",
                    "participations": [
                        {
                            "partPid": "1", //
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "0", //是否开启 0未开启，1已开启
                            "promptNumber": "9+" //数量
                        },
                        {
                            "partPid": "2",
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "1",
                            "promptNumber": "9+"
                        },
                        {
                            "partPid": "3", //
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "0", //是否开启 0未开启，1已开启
                            "promptNumber": "9+" //数量
                        },
                        {
                            "partPid": "4",
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "1",
                            "promptNumber": "9+"
                        },
                        {
                            "partPid": "5", //
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "0", //是否开启 0未开启，1已开启
                            "promptNumber": "9+" //数量
                        },
                        {
                            "partPid": "6",
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "1",
                            "promptNumber": "9+"
                        },
                        {
                            "partPid": "7", //
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "0", //是否开启 0未开启，1已开启
                            "promptNumber": "9+" //数量
                        },
                        {
                            "partPid": "8",
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "1",
                            "promptNumber": "9+"
                        },
                        {
                            "partPid": "9", //
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "0", //是否开启 0未开启，1已开启
                            "promptNumber": "9+" //数量
                        },
                        {
                            "partPid": "10",
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "1",
                            "promptNumber": "9+"
                        },
                        {
                            "partPid": "11", //
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "0", //是否开启 0未开启，1已开启
                            "promptNumber": "9+" //数量
                        },
                        {
                            "partPid": "12",
                            "prodPic": "http://ecx.images-amazon.com/images/I/61CTlvCIexL._SL1000_.jpg",
                            "prodTitle": "Fire HD 8, 8\" HD Display, Wi-Fi, 8 GB - Includes Special Offers, Black",
                            "prodUrl": "http://www.amazon.com/gp/product/B00S5HFVGI?redirect=true&ref_=kin_comp_dk_th_img",
                            "isOpen": "1",
                            "promptNumber": "9+"
                        }
                    ]
                }
            };

            callBack && callBack(data);
            */
            ////////////////////////////以上是前端测试效果用////////////////////////////
        },

        //更改开启和关闭提示信息
        'changeRemindData': function( settings, callBack ) {
            ///////////////////////////以下是前端测试效果用////////////////////////////

            this.common({
                'type': 'POST',
                'dataType': 'json',
                'url': '/participation/updateOpen',
                'data': {
                    //需要抓取数据的url
                    'partPid':settings.partPid,
                    'isOpen':settings.isOpen
                },
                'callBack': callBack
            });

            //期望返回的json数据格式如下
            /*
            var data = {
                "code": 200,
                "description": "sucess"
            };
            callBack && callBack(data);
            */
            ////////////////////////////以上是前端测试效果用////////////////////////////
        }

    };

    window.partinprodServer = partinprodServer;

})( jQuery );